#include "imagerotator.h"
#include "ui_imagerotator.h"

ImageRotator::ImageRotator(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::ImageRotator)
    , myTimerId(0)
    , currentImageIndex(0)
{
    ui->setupUi(this);


}

ImageRotator::~ImageRotator()
{
    delete ui;
}

void ImageRotator::on_startButton_clicked()
{
    if (myTimerId == 0 && !importedImages.isEmpty()) {
        myTimerId = this->startTimer(TIMEOUT);
    }

}

void ImageRotator::importImages() {
    QStringList filePaths = QFileDialog::getOpenFileNames(this, "导入图片", "", "Images (*.png *.jpg *.jpeg)");
    importedImages.clear(); // 清空之前的图片列表
    foreach (const QString &filePath, filePaths) {
        QPixmap pix(filePath);
        if (!pix.isNull()) {
            importedImages.append(pix); // 保存QPixmap对象到列表
        }
    }
    if (!importedImages.isEmpty()) {
        currentImageIndex = 0; // 初始化索引为第一张图片
         ui->label->setPixmap(importedImages[currentImageIndex]);
    }
}

void ImageRotator::timerEvent(QTimerEvent *event)
{

    if (event->timerId() == myTimerId) {
        if (currentImageIndex < importedImages.size()) {
            ui->label->setPixmap(importedImages[currentImageIndex]);
            currentImageIndex++;
            ui->currentLabel->setText(QString::number(currentImageIndex));
            ui->sumLabel->setText(QString::number(importedImages.size()));
        } else {
            currentImageIndex = 0; // 如果到达列表末尾，则从头开始
        }
    }
}



void ImageRotator::on_stopButton_clicked()
{
    if (myTimerId != 0) {
        this->killTimer(myTimerId);
        myTimerId = 0;
    }
}


void ImageRotator::on_back2wButton_clicked()
{

        this->hide();
        Widget *w=new Widget;
        w->show();

}


void ImageRotator::on_importButton_clicked()
{
  importImages();
}

